package com.examsage.examsage.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.examsage.examsage.model.MajorApplication;
import com.examsage.examsage.mapper.MajorApplicationMapper;
import com.examsage.examsage.model.vo.ResponseVO;
import com.examsage.examsage.service.IMajorApplicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author examsage
 * @since 2025-07-02
 */
@Service
public class MajorApplicationServiceImpl extends ServiceImpl<MajorApplicationMapper, MajorApplication> implements IMajorApplicationService {

    @Resource
    private MajorApplicationMapper majorApplicationMapper;

    @Override
    public ResponseVO selectMajorApplication(int page, int size, String studentName, String idCardNo, int majorPlanId, String status) {
        // 默认值处理
        if (page <= 0) page = 1;
        if (size <= 0) size = 10;

        QueryWrapper<MajorApplication> queryWrapper = new QueryWrapper<>();

        // 按考生姓名模糊查询
        if (studentName != null && !studentName.trim().isEmpty()) {
            queryWrapper.like("student_name", studentName);
        }

        // 按身份证号精确查询
        if (idCardNo != null && !idCardNo.trim().isEmpty()) {
            queryWrapper.eq("id_card_no", idCardNo);
        }

        // 按专业计划 ID 查询
        if (majorPlanId > 0) {
            queryWrapper.eq("major_plan_id", majorPlanId);
        }

        // 按审核状态筛选（枚举值：PENDING, APPROVED, REJECTED）
        if (status != null && !status.trim().isEmpty()) {
            queryWrapper.eq("status", status.toUpperCase());
        }

        // 分页查询
        Page<MajorApplication> pageQuery = new Page<>(page, size);

        ResponseVO responseVO = new ResponseVO();

        try {
            Page<MajorApplication> resultPage = majorApplicationMapper.selectPage(pageQuery, queryWrapper);
            responseVO.setStatusCode(200);
            responseVO.setMessage("查询专业申请成功");
            responseVO.setData(resultPage);
        } catch (Exception e) {
            responseVO.setStatusCode(500);
            responseVO.setMessage("查询专业申请失败");
            responseVO.setData(null);
        }

        return responseVO;
    }

    @Override
    public ResponseVO approveMajorApplication(int id, String notes) {
        ResponseVO responseVO = new ResponseVO();

        try {
            // 根据 id 查询 MajorApplication 记录
            MajorApplication majorApplication = this.getById(id);
            if (majorApplication == null) {
                responseVO.setStatusCode(404);
                responseVO.setMessage("未找到该专业申请记录");
                return responseVO;
            }

            // 更新 status 为 "APPROVED"
            majorApplication.setStatus("APPROVED");
            majorApplication.setApprovalNotes(notes);
            // 更新数据库
            boolean updateResult = this.updateById(majorApplication);
            if (updateResult) {
                responseVO.setStatusCode(200);
                responseVO.setMessage("批准专业申请成功");
            } else {
                responseVO.setStatusCode(500);
                responseVO.setMessage("批准专业申请失败");
            }
        } catch (Exception e) {
            responseVO.setStatusCode(500);
            responseVO.setMessage("批准专业申请时发生错误: " + e.getMessage());
        }

        return responseVO;
    }

    @Override
    public ResponseVO rejectMajorApplication(int id, String notes) {
        ResponseVO responseVO = new ResponseVO();

        try {
            // 根据 id 查询 MajorApplication 记录
            MajorApplication majorApplication = this.getById(id);
            if (majorApplication == null) {
                responseVO.setStatusCode(404);
                responseVO.setMessage("未找到该专业申请记录");
                return responseVO;
            }

            // 更新 status 为 "REJECTED"
            majorApplication.setStatus("REJECTED");
            majorApplication.setApprovalNotes(notes);
            // 更新数据库
            boolean updateResult = this.updateById(majorApplication);
            if (updateResult) {
                responseVO.setStatusCode(200);
                responseVO.setMessage("拒绝专业申请成功");
            } else {
                responseVO.setStatusCode(500);
                responseVO.setMessage("拒绝专业申请失败");
            }
        } catch (Exception e) {
            responseVO.setStatusCode(500);
            responseVO.setMessage("拒绝专业申请时发生错误: " + e.getMessage());
        }

        return responseVO;
    }



}

